package com.efounder.frame.activity;

import android.content.Intent;
import android.net.http.SslError;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.efounder.constant.EnvironmentVariable;
import com.efounder.frame.utils.OSPPlugin;
import com.efounder.mobilemanager.R;

import static com.efounder.frame.utils.Constants.CHAT_USER_ID;
import static com.efounder.frame.utils.Constants.KEY_SETTING_APPID;


public class WebViewActivity extends AppCompatActivity {

    public WebView webView;
    public Boolean ReashPage = true;
    public Boolean PicReady = false;
    public String fileSavePath = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview_main);


        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();

        String username = bundle.getString("username");
        String userpass = bundle.getString("userpass");
        String urlstr = bundle.getString("urlstr");

        //加载页面
        webView = (WebView) findViewById(R.id.webviewactivty);
        WebSettings webSettings = webView.getSettings();

        webView.clearCache(true);
        webView.clearHistory();

        //允许JavaScript执行
        webSettings.setJavaScriptEnabled(true);

        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAppCacheEnabled(true);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);


//        webView.setWebViewClient(new WebViewClient() {
//            public void onPageFinished(WebView view, String url) {
//                webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
//            }
//        });
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                view.loadUrl(url);
                return true;
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
                Toast.makeText(WebViewActivity.this, message,
                        Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        webView.setWebViewClient(new WebViewClient() {
            public void onReceivedSslError(WebView view,
                                           SslErrorHandler handler, SslError error) {
                //handler.cancel(); 默认的处理方式，WebView变成空白页
                handler.proceed(); // 接受证书
            }
        });

        DisplayMetrics dm = new DisplayMetrics();//获取当前显示的界面大小
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;//获取当前界面的高度
        RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) webView.getLayoutParams();
        linearParams.height = height;
        webView.setLayoutParams(linearParams);

        //找到Html文件，也可以用网络上的文件
        if (urlstr.toLowerCase().startsWith("http")) {
            webView.loadUrl(urlstr);
        } else {
            String baseUrl = "file:///mnt/sdcard/" + EnvironmentVariable.getProperty(KEY_SETTING_APPID) + "/" + EnvironmentVariable.getProperty(CHAT_USER_ID) + "/Apps/" + urlstr;
            webView.loadUrl(baseUrl);
        }
        // 添加一个对象, 让JS可以访问该对象的方法, 该对象中可以调用JS中的方法
        webView.addJavascriptInterface(new OSPPlugin(username, userpass, this), "OSPPlugin");
    }

    public void onResume() {
        super.onResume();
        if (ReashPage) {
            webView.reload();
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == 1) {
                    fileSavePath = data.getStringExtra("path");
                    PicReady = true;
                } else {
                    fileSavePath = "";
                    PicReady = true;
                }
        }
    }
}

